package com.dykj.fireFighting.web.utils;

import org.jxls.builder.xls.XlsCommentAreaBuilder;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * @Author:zhujunming
 * @Date: 2019/6/24 8:19
 * @For: 文档导出
 **/
public class ExportUtils {
    static {
        XlsCommentAreaBuilder.addCommandMapping("merge", MergeCommand.class);
    }

    public static void exportExcel(Context context, String modelName, String returnName, HttpServletResponse response) throws Exception {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("excelTemplate/" + modelName);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        JxlsHelper.getInstance().setUseFastFormulaProcessor(false).processTemplate(is, os, context);
        response.setContentType("application/vnd.ms-excel");
        String name = java.net.URLEncoder.encode(returnName, "UTF-8");
        response.setHeader("Content-disposition", "filename=\"" + name + "\"");
        // 获得response中的输出流
        ServletOutputStream sout = response.getOutputStream();
        os.writeTo(sout);
        os.close();
        sout.flush();
        sout.close();

    }
}
